from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.base import Model
from django.db.models.deletion import PROTECT, CASCADE, DO_NOTHING
import django.utils.timezone as timezone


class User(AbstractUser):
    class Usertype(models.IntegerChoices):
        CG = 1  # 超管
        PG = 1000  # 普通管理员
        XS = 2000  # 学生
        LS = 3000  # 老师
    # 重新定义id字段类型，以防用户数量巨多
    id = models.BigAutoField(primary_key=True)
    usertype = models.IntegerField('用户类型', choices=Usertype.choices)
    realname = models.CharField('真实姓名', max_length=30, db_index=True)
    studentno = models.CharField(
        '学号',
        max_length=10, 
        db_index=True, 
        null=True, blank=True)
    desc = models.CharField('备注描述', max_length=500, null=True, blank=True)
    # 在创建用户时提醒输入不能为空的字段
    REQUIRED_FIELDS = ['usertype', 'realname']


class Notice(models.Model):
    class Status(models.IntegerChoices):
        ZC = 1  # 正常发布状态
        CH = 2  # 撤回状态
        FJ = 3  # 封禁状态
    title = models.CharField('标题', max_length=100, db_index=True)
    content = models.TextField('内容')
    author = models.ForeignKey(User, PROTECT, verbose_name='作者')
    status = models.IntegerField('状态', choices=Status.choices, default=Status.ZC)
    pubdate = models.DateTimeField('发布时间', default = timezone.now)
    modidate = models.DateTimeField('修改时间', auto_now=True)
    class Meta:
        verbose_name = '通知'
        verbose_name_plural = '通知' 


class News(models.Model):
    class Status(models.IntegerChoices):
        ZC = 1  # 正常发布状态
        CH = 2  # 撤回状态
        FJ = 3  # 封禁状态
    title = models.CharField('标题', max_length=100, db_index=True)
    content = models.TextField('内容')
    author = models.ForeignKey(User, PROTECT, verbose_name='作者')
    status = models.IntegerField('状态', choices=Status.choices, default=Status.ZC)
    pubdate = models.DateTimeField('发布时间', default=timezone.now)
    modidate = models.DateTimeField('修改时间', auto_now=True)
    top = models.IntegerField('置顶顺序', blank=True, null=True)
    class Meta:
        verbose_name = '新闻'
        verbose_name_plural = '新闻' 


class Paper(models.Model):
    class Status(models.IntegerChoices):
        ZC = 1  # 正常发布状态
        CH = 2  # 撤回状态
        FJ = 3  # 封禁状态
    title = models.CharField('标题', max_length=100, db_index=True)
    content = models.TextField('内容')
    author = models.ForeignKey(User, PROTECT, verbose_name='作者')
    status = models.IntegerField('状态', choices=Status.choices, default=Status.ZC)
    pubdate = models.DateTimeField('发布时间', default=timezone.now)
    modidate = models.DateTimeField('修改时间', auto_now=True)
    thumbupcount = models.BigIntegerField('点赞数', default=0)
    class Meta:
        verbose_name = '论文'
        verbose_name_plural = '论文' 


class Config(models.Model):
    name = models.CharField('页面名称', max_length=50, unique=True)
    value = models.TextField('页面配置')


class StuTeaRelation(models.Model):
    student = models.OneToOneField(User, CASCADE, verbose_name='学生')
    teacher = models.ForeignKey(User, CASCADE, related_name='teasturelation', verbose_name='老师')


class Thumbup(models.Model):
    user = models.ForeignKey(User, DO_NOTHING, verbose_name='点赞人')
    paper = models.ForeignKey(Paper, CASCADE, verbose_name='论文')
    thumbup = models.BooleanField('是否点赞', default=True)


class WfDesign(models.Model):
    creator = models.ForeignKey(User, CASCADE, verbose_name='创建者')
    title = models.CharField('主题', max_length=100, db_index=True)
    currentstate = models.CharField('当前状态', max_length=50, db_index=True)
    createdate = models.DateTimeField('创建时间', auto_now_add=True)
    class Meta:
        ordering = ['-createdate']


class WfDesignStep(models.Model):
    wf = models.ForeignKey(WfDesign, CASCADE, verbose_name='工作流')
    operator = models.ForeignKey(User, CASCADE, verbose_name='操作者')
    actiondate = models.DateTimeField('操作时间', auto_now_add=True)
    actionname = models.CharField('操作名称', max_length=50)
    nextstate = models.CharField('下步操作', max_length=50)
    submitdata = models.TextField('提交数据', default='')
    class Meta:
        ordering = ['actiondate']
